package com.simon.app.utils;

import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Build;

import androidx.core.app.NotificationCompat;

import com.simon.app.R;

// 实现一个通知的功能
// 返回值：通知id(notificationId)
// 使用方法：
//      在Activity内使用NotificationHelper notificationHelper = new NotificationHelper(this)实例化此类
// 需要this参数
// 必要权限：<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
// 示例  1、 自定义渠道： NotificationHelper notificationHelper2 = new NotificationHelper(this,"渠道2","描述2","channel_2");
//          默认渠道：   NotificationHelper notificationHelperDefault = new NotificationHelper(this);
//          或使用      NotificationHelper.setIntent(intent);
//      2、 发送通知：（后两个可选参数） notificationHelper.sendNotification("标题2","通知10",-1,intent);


public class NotificationHelper {
    private static String CHANNEL_ID = "channelId1";
    private final NotificationManager notificationManager;
    private Context context;
    private PendingIntent m_PendingIntent;
    private Intent m_intent;
    private static int defaultNotificationId = Integer.MIN_VALUE;

    //设置intent来确认需要跳转的页面
    public void setIntent(Intent intent) {
        this.m_intent = intent;
        m_PendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE);
    }

    //如不创建渠道则使用默认渠道
    public NotificationHelper(Context context) {
        this(context, "默认渠道", "这里是默认的通知渠道", "default_channel_id");
    }

    public NotificationHelper(Context context, String channelNameIn, String channelDescriptionIn, String channelIdIn) {
        CHANNEL_ID = channelIdIn;
        this.context = context;
        this.notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        createNotificationChannel(channelNameIn, channelDescriptionIn);
    }

    //若不指定id则使用自增id
    public void sendNotification(String title, String text) {
        sendNotification(title, text, defaultNotificationId, null);
        defaultNotificationId++;
    }

    //指定id则使用以下方法
    public void sendNotification(String title, String text, int notficationId, Intent intent) {
        if (notficationId < 0) {
            notficationId = defaultNotificationId;
            if (defaultNotificationId >= -1) {
                defaultNotificationId = Integer.MIN_VALUE;
            } else {
                defaultNotificationId++;
            }
        }

        PendingIntent l_pendingIntent = null;
        if (intent != null) {
            l_pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE);
        }

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            Notification.Builder builder = null;
            builder = new Notification.Builder(context, CHANNEL_ID)
                    .setSmallIcon(R.drawable.notification_icon)
                    .setContentTitle(title)
                    .setContentText(text);

            //当pendingIntent不会空值时才设置
            if (m_PendingIntent != null) {
                builder.setContentIntent(m_PendingIntent);
            }

            if (l_pendingIntent != null) {
                builder.setContentIntent(l_pendingIntent);
            }

            builder.setAutoCancel(true);
            NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
            notificationManager.notify(notficationId, builder.build());//如出现报错则位声明权限

        } else {          //若小于Android8.0则执行以下代码
            NotificationCompat.Builder builder = null;
            builder = new NotificationCompat.Builder(context)
                    .setSmallIcon(R.drawable.notification_icon)
                    .setContentTitle(title)
                    .setContentText(text);
            NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
            notificationManager.notify(notficationId, builder.build());//如出现报错则位声明权限
        }
    }

    //创建通知渠道
    private void createNotificationChannel(String channelNameIn, String channelDescriptionIn) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            CharSequence channelName = channelNameIn;
            String channelDescription = channelDescriptionIn;
            int importance = NotificationManager.IMPORTANCE_DEFAULT;
            NotificationChannel channel = new NotificationChannel(CHANNEL_ID, channelName, importance);
            channel.setDescription(channelDescription);
            channel.enableLights(true);
            channel.setLightColor(Color.RED);
            notificationManager.createNotificationChannel(channel);
        }
    }
}
